package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 供应商管理Controller
 */
@Controller
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 条件查询供应商列表
     *
     * @param
     * @return
     */
    @PostMapping("/supplier/list")
    @ResponseBody
    public Map<String, Object> supplierList(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "30") Integer rows,
                                            @RequestParam(required = false) String supplierName) {

        Map<String, Object> map = supplierService.getSupplierList(page, rows, supplierName);
        return map;
    }

    /**
     * 新增供应商信息
     */
    @PostMapping("/supplier/save")
    @ResponseBody
    public ServiceVO saveOrUpdate(Supplier supplier) {

        if (supplier == null) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }

        supplierService.saveOrUpdate(supplier);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除供货商信息
     */
    @PostMapping("/supplier/delete")
    @ResponseBody
    public ServiceVO supplierDelete(@RequestParam String ids) {
        // "1,2,3"
//        String ids = request.getParameter("ids");

        if (StringUtils.isEmpty(ids)) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }

        supplierService.deleteByIds(ids);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


}
